import { Methods } from "./request";
import router from "../router";

export function controller(target: any) {
  // console.log(Object.getOwnPropertyNames(target.prototype));
  Object.getOwnPropertyNames(target.prototype).forEach((value) => {
    const path = Reflect.getMetadata("path", target.prototype, value);
    const method: Methods = Reflect.getMetadata(
      "method",
      target.prototype,
      value
    );
    const middlewares = Reflect.getMetadata(
      "middlewares",
      target.prototype,
      value
    );
    if (path && method && target.prototype[value]) {
      if (middlewares) {
        router[method](path, ...middlewares, target.prototype[value]);
      } else {
        router[method](path, target.prototype[value]);
      }
    }
  });
}
